//
//  JMWeChat.m
//  JMLibrary
//
//  Created by zhengxianda on 2018/7/24.
//

#import "JMWeChat.h"

#import <WechatOpenSDK/WXApiObject.h>

@implementation JMWeChat

- (void)registerApp:(NSString *)appId{
    [WXApi registerApp:appId];
}

- (BOOL)handleOpenURL:(NSURL *)url{
    return [WXApi handleOpenURL:url delegate:self];
}

- (void)shareToChannel:(JMShareChannel)channel messageObject:(JMShareMessageObject *)messageObject handler:(JMShareRequestHandler)handler{
    [super shareToChannel:channel messageObject:messageObject handler:handler];
    
    SendMessageToWXReq *request = [[SendMessageToWXReq alloc] init];
    
    if (messageObject.musicDataURL && messageObject.musicPageURL) {
        request.message = [WXMediaMessage message];
        request.message.thumbData = messageObject.thumbImage;
        request.message.title = messageObject.title;
        request.message.description = messageObject.descriptions;
        
        WXMusicObject * object = [WXMusicObject object];
        object.musicUrl = [messageObject.musicPageURL absoluteString];
        object.musicDataUrl = [messageObject.musicDataURL absoluteString];
        request.message.mediaObject = object;
        request.bText = NO;
    }else if (messageObject.webPageURL) {
        request.message = [WXMediaMessage message];
        request.message.thumbData = messageObject.thumbImage;
        request.message.title = messageObject.title;
        request.message.description = messageObject.descriptions;
        
        WXWebpageObject * object = [WXWebpageObject object];
        object.webpageUrl = [messageObject.webPageURL absoluteString];
        request.message.mediaObject = object;
        request.bText = NO;
    }else if (messageObject.shareImage) {
        request.message = [WXMediaMessage message];
        request.message.thumbData = messageObject.thumbImage;
        request.message.title = messageObject.title;
        request.message.description = messageObject.descriptions;
        
        WXImageObject * object = [WXImageObject object];
        object.imageData = messageObject.shareImage;
        request.message.mediaObject = object;
        request.bText = NO;
    }else{
        request.text = messageObject.text;
        request.bText = YES;
    }
    
    switch (channel) {
        case JMShareChannelWeChatMessage:
            request.scene = WXSceneSession;
            break;
        case JMShareChannelWeChatTimeline:
            request.scene = WXSceneTimeline;
            break;
        default:
            break;
    }
    
    BOOL result = [WXApi sendReq:request];
    if (!result) {
        [self callHandlerWithCode:WXErrCodeSentFail];
    }
}

- (NSString *)errorMsgWithCode:(int)code{
    NSString *msg = @"";
    switch (code) {
        case WXSuccess:
            msg = @"成功";
            break;
        case WXErrCodeCommon:
            msg = @"普通错误类型";
            break;
        case WXErrCodeUserCancel:
            msg = @"用户点击取消并返回";
            break;
        case WXErrCodeSentFail:
            msg = @"发送失败";
            break;
        case WXErrCodeAuthDeny:
            msg = @"授权失败";
            break;
        case WXErrCodeUnsupport:
            msg = @"微信不支持";
            break;
    }
    return msg;
}

- (void)onReq:(BaseReq *)req{
    
}

- (void)onResp:(BaseResp *)resp{
    [self callHandlerWithCode:resp.errCode];
}

@end
